package com.edu.cmsservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.cmsservice.entity.CrmBanner;
import com.edu.cmsservice.service.CrmBannerService;
import com.edu.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * 后台
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
@CrossOrigin
public class CrmBannerController {
    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取banner分页列表")
    @GetMapping("/{page}/{limit}")
    public R index(
                   @ApiParam(value = "page",name = "page",required = true)
                   @PathVariable("/page") Long page,
                   @ApiParam(value = "limit",name = "limit",required = true)
                   @PathVariable("/limit") Long limit){
        Page<CrmBanner> pageParam =new Page<CrmBanner>(page,limit);

        crmBannerService.pageBanner(pageParam,null);
        return R.ok().data("rows",pageParam.getRecords()).data("total",pageParam.getTotal());

        }

    /**
     * 根据id查询banner
     * @param id
     * @return
     */
    @ApiOperation(value = "获取Banner")
        @GetMapping("get/{id}")
        public R get(@ApiParam(value = "id",name = "id",required = true)
                     @PathVariable("id") String id){
             CrmBanner crmBanner=   crmBannerService.getBannerById(id);

             return R.ok().data("banner",crmBanner);
        }

    /**
     * 新增banner
     */
    @ApiOperation("新增Banner")
        @PostMapping("/save")
    public R save(@ApiParam(value = "crmBanner",name = "crmBanner",required = true)
                  @RequestBody CrmBanner crmBanner){

        crmBannerService.save(crmBanner);
        return R.ok();
    }

    /**
     * 修改banner
     * @param crmBanner
     * @return
     */
    @ApiOperation("修改banner")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "crmBanner",name = "crmBanner",required = true)
                        @RequestBody CrmBanner crmBanner){
        crmBannerService.updateBannerById(crmBanner);
            return R.ok();
    }

    @ApiOperation("删除banner")
    @DeleteMapping("remove/{id}")
    public R remove(@ApiParam(value = "crmBanner",name = "crmBanner",required = true)
                    @PathVariable String id){
    crmBannerService.removeBannerById(id);
    return R.ok();

    }

}
